package com.lsfb.signsys.compatible;

import android.support.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by zgscwjm on 2018/3/27.
 */

public class MyConverter extends Converter.Factory {
    private final Gson gson;

    public MyConverter(Gson gson) {
        this.gson = gson;
    }

    public static MyConverter create(){
        return create(new Gson());
    }

    private static MyConverter create(Gson gson) {
        if (gson == null) throw new NullPointerException("gson is null");
        return new MyConverter(gson);
    }


    @Nullable
    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {

        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));

        return new MyGsonResponseBodyCoverter<>(gson,adapter);
    }



}
